//
//  HomeViewController.m
//  LINNER
//
//  Created by Kendama on 2017/10/18.
//  Copyright © 2017年 Kendama. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeLeftViewController.h"
#import "LightningViewController.h"
//test
#import "LinnerViewController.h"

#import "SWRevealViewController.h"
#import "UIViewController+LGSideMenuController.h"
#import "BluetoothConnectViewController.h"
#import "LinnerControlViewController.h"

@interface HomeViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation HomeViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self setNavigationStyle];
    [self.tableView reloadData];
    //    SWRevealViewController *revealController = [self revealViewController];
    //    [revealController panGestureRecognizer];
    //    [revealController tapGestureRecognizer];
    //
    //    [self.leftButton addTarget:revealController action:@selector(revealToggle:) forControlEvents:UIControlEventTouchUpInside];
    self.sideMenuController.leftViewEnabled = YES;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTableView) name:@"lightingconnect" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTableView) name:@"lightingDisconnect" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTableView) name:@"linnerConnect" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTableView) name:@"DislinnerConnect" object:nil];
}

- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    //    SWRevealViewController *revealController = [self revealViewController];
    //
    //    [revealController removeAllGestureRecognizer];
    self.sideMenuController.leftViewEnabled = NO;
    
    
}


#pragma mark - --------------------------view--------------------------

- (void)updateTableView{
    
    [self.tableView reloadData];
    
}


- (void)onLeftButtonClick:(id)sender{
    [self.sideMenuController showLeftViewAnimated:YES completionHandler:nil];
    
    //    UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    //    HomeLeftViewController *VC = [mainStoryboard instantiateViewControllerWithIdentifier:@"HomeLeftViewController"];
    //    [self.navigationController pushViewController:VC animated:YES];
    
}

- (void)setNavigationStyle{
    
    [self setNavigationBarTitle:MyNSLocalizedString(@"产品选择")];
    [self setLeftButtonImage:[UIImage imageNamed:@"icon_set_nor"]];
    [self.navigationController.navigationBar makeTransparent_nornav];
    
}

#pragma mark - tableView Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 130;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
    
}
#pragma mark - tableView Delegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"menuViewCell0_%ld",(long)indexPath.row]];
#pragma mark -   lable.tag = 10 UIimage.tag = 11; connctLabel.tag = 12 connectImage.tag = 13 connectlabel = 20
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UILabel *deviceName = (UILabel *)[cell viewWithTag:10];
    if (indexPath.section == 0) {
        
        deviceName.text = MyNSLocalizedString(@"NC21 Lightning");
        
    }else{
        
        deviceName.text = MyNSLocalizedString(@"NC50 Pro");
    }
    
    UIImageView *deviceImage = (UIImageView *)[cell viewWithTag:11];
    
    if (indexPath.section == 0) {
        
        deviceImage.image = [UIImage imageNamed:@"bg_lightning"];
        
    }else{
        
        deviceImage.image = [UIImage imageNamed:@"bg_mo"];
        
    }
    
    UILabel *connectLabel = (UILabel *)[cell viewWithTag:20];
    
    UIImageView *connectImage = (UIImageView *)[cell viewWithTag:13];
    
    if (indexPath.section == 0) {
        
        if ([AppDelegate globalDelegate].lightingConnet == YES) {
            
            [connectImage setHighlighted:YES];
            connectLabel.text = MyNSLocalizedString(@"已连接");
            
        }else{
            
            [connectImage setHighlighted:NO];
            connectLabel.text = MyNSLocalizedString(@"未连接");
            
        }
    }else{
        
        
        if ([AppDelegate globalDelegate].linnerConnet == YES) {
            
            [connectImage setHighlighted:YES];
            connectLabel.text = MyNSLocalizedString(@"已连接");
            
        }else{
            
            [connectImage setHighlighted:NO];
            connectLabel.text = MyNSLocalizedString(@"未连接");
            
        }
        
        
    }
    
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        
        if ([AppDelegate globalDelegate].lightingConnet == YES) {
            
            UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            LightningViewController *VC = [mainStoryboard instantiateViewControllerWithIdentifier:@"LightningViewController"];
            [self.navigationController pushViewController:VC animated:YES];
            
            
        }else{
            
            [SVProgressHUD showInfoWithStatus:MyNSLocalizedString(@"请连接耳机")];
            
            
        }
        
        
    }else{//mo
        
        
        
        
        if ([AppDelegate globalDelegate].linnerConnet == YES) {
            
            LinnerControlViewController *vc = [[LinnerControlViewController alloc] init];
            //            LinnerViewController *vc = [LinnerViewController new];
            [self.navigationController pushViewController:vc animated:YES];
            
        }else{
            
            BluetoothConnectViewController *vc = [[BluetoothConnectViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
            //        #define iOS10 ([[UIDevice currentDevice].systemVersion doubleValue] >= 10.0)
            //            NSString * urlString = @"App-Prefs:root=Bluetooth";
            //            if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlString]]) {
            //                if (iOS10) {
            //                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:nil];
            //                } else {
            //                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
            //                }
            //            }
            
            
        }
        
        
        
    }
    
}

@end
